/*
 * Copyright (C) 2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#include <stdlib.h>
#include <check.h>
#include "PlanetWars2dRT-core/specific/model/game_state_basic.h"


struct game_state_basic_t a_state;

void
tests_setup_init(void)
{
  game_state_basic_init_first_time_only(&a_state);
}

void
tests_setup_free(void)
{
  game_state_basic_destructor(&a_state);
}


START_TEST(test_set_pause)
{
  game_state_basic_set_pause(&a_state, true);
  ck_assert(game_state_basic_is_in_pause(&a_state));
  game_state_basic_set_pause(&a_state, true);
  ck_assert(game_state_basic_is_in_pause(&a_state));
  
  game_state_basic_set_pause(&a_state, false);
  ck_assert(!game_state_basic_is_in_pause(&a_state));
  game_state_basic_set_pause(&a_state, false);
  ck_assert(!game_state_basic_is_in_pause(&a_state));
  
  game_state_basic_set_pause(&a_state, true);
  ck_assert(game_state_basic_is_in_pause(&a_state));
  
  game_state_basic_set_pause(NULL, true);
  game_state_basic_set_pause(NULL, false);
}
END_TEST

START_TEST(test_toogle_pause)
{
  game_state_basic_set_pause(&a_state, true);
  ck_assert(game_state_basic_is_in_pause(&a_state));
  game_state_basic_toogle_pause(&a_state);
  ck_assert(!game_state_basic_is_in_pause(&a_state));
  game_state_basic_toogle_pause(&a_state);
  ck_assert(game_state_basic_is_in_pause(&a_state));
  
  game_state_basic_set_pause(&a_state, false);
  ck_assert(!game_state_basic_is_in_pause(&a_state));
  game_state_basic_toogle_pause(&a_state);
  ck_assert(game_state_basic_is_in_pause(&a_state));
  game_state_basic_toogle_pause(&a_state);
  ck_assert(!game_state_basic_is_in_pause(&a_state));
  
  game_state_basic_toogle_pause(NULL);
}
END_TEST


START_TEST(test_reduce_update_speed_too_much)
{
  ck_assert(!game_state_basic_reduce_update_speed(&a_state,
					    game_state_basic_get_update_speed(&a_state)));
  ck_assert(!game_state_basic_reduce_update_speed(&a_state, 1));
}
END_TEST

START_TEST(test_reduce_update_speed_to_minimum)
{
  ck_assert(game_state_basic_reduce_update_speed(&a_state,
					   game_state_basic_get_update_speed(&a_state)
					   - 1));
  ck_assert(!game_state_basic_reduce_update_speed(&a_state, 1));
}
END_TEST

START_TEST(test_reduce_update_speed_decrement)
{
  unsigned short tmp1, tmp2;
  
  tmp1 = game_state_basic_get_update_speed(&a_state);
  do
    {
      tmp2 = game_state_basic_get_update_speed(&a_state);
      ck_assert_uint_le(tmp2, tmp1);
      tmp1 = tmp2;
    }
  while (game_state_basic_reduce_update_speed(&a_state, 1));
  
  ck_assert_uint_eq(tmp1, 1);
  ck_assert(!game_state_basic_reduce_update_speed(&a_state, 1));
  ck_assert_uint_eq(tmp1, 1);
}
END_TEST

START_TEST(test_decrement_update_speed)
{
  unsigned short tmp1, tmp2;
  
  tmp1 = game_state_basic_get_update_speed(&a_state);
  do
    {
      tmp2 = game_state_basic_get_update_speed(&a_state);
      ck_assert_uint_le(tmp2, tmp1);
      tmp1 = tmp2;
    }
  while (game_state_basic_decrement_update_speed(&a_state));
  
  ck_assert_uint_eq(tmp1, 1);
  ck_assert(!game_state_basic_decrement_update_speed(&a_state));
  ck_assert_uint_eq(tmp1, 1);
}
END_TEST

START_TEST(test_increment_update_speed)
{
  unsigned short tmp1, tmp2;
  
  tmp1 = game_state_basic_get_update_speed(&a_state);
  game_state_basic_increment_update_speed(&a_state);
  tmp2 = game_state_basic_get_update_speed(&a_state);
  ck_assert_uint_lt(tmp1, tmp2);
  game_state_basic_increment_update_speed(&a_state);
  tmp1 = game_state_basic_get_update_speed(&a_state);
  ck_assert_uint_lt(tmp2, tmp1);
}
END_TEST


START_TEST(test_quit)
{
  game_state_basic_set_quit(&a_state, true);
  ck_assert(game_state_basic_has_to_quit(&a_state));
  game_state_basic_set_quit(&a_state, true);
  ck_assert(game_state_basic_has_to_quit(&a_state));
  
  game_state_basic_set_quit(&a_state, false);
  ck_assert(!game_state_basic_has_to_quit(&a_state));
  game_state_basic_set_quit(&a_state, false);
  ck_assert(!game_state_basic_has_to_quit(&a_state));
  
  game_state_basic_set_quit(&a_state, true);
  ck_assert(game_state_basic_has_to_quit(&a_state));
  
  game_state_basic_set_quit(NULL, true);
  game_state_basic_set_quit(NULL, false);
}
END_TEST

START_TEST(test_equals)
{
  ck_assert(game_state_basic_equals (&a_state, &a_state));
  ck_assert(game_state_basic_equals (NULL,     NULL));
  ck_assert(!game_state_basic_equals(&a_state, NULL));
  ck_assert(!game_state_basic_equals(NULL,     &a_state));
}
END_TEST


Suite*
game_state_basic_suite(void)
{
  Suite* s = suite_create("Game state");
  
  TCase* tc_pause = tcase_create("Pause");
  tcase_add_checked_fixture(tc_pause,
			    tests_setup_init, tests_setup_free);
  tcase_add_test(tc_pause, test_set_pause);
  tcase_add_test(tc_pause, test_toogle_pause);
  suite_add_tcase(s, tc_pause);
  
  TCase* tc_update_speed = tcase_create("Update speed");
  tcase_add_checked_fixture(tc_update_speed,
			    tests_setup_init, tests_setup_free);
  tcase_add_test(tc_update_speed, test_reduce_update_speed_too_much);
  tcase_add_test(tc_update_speed, test_reduce_update_speed_to_minimum);
  tcase_add_test(tc_update_speed, test_reduce_update_speed_decrement);
  tcase_add_test(tc_update_speed, test_decrement_update_speed);
  tcase_add_test(tc_update_speed, test_increment_update_speed);
  suite_add_tcase(s, tc_update_speed);
  
  TCase* tc_others = tcase_create("Other(s)");
  tcase_add_checked_fixture(tc_others,
			    tests_setup_init, tests_setup_free);
  tcase_add_test(tc_others, test_quit);
  tcase_add_test(tc_others, test_equals);
  suite_add_tcase(s, tc_others);
  
  return s;
}

int
main(void)
{
  size_t number_failed;
  Suite* s;
  SRunner* sr;
  
  s = game_state_basic_suite();
  sr = srunner_create(s);
  
  srunner_run_all(sr, CK_NORMAL);
  number_failed = srunner_ntests_failed(sr);
  srunner_free(sr);
  
  return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
